//package work.rightAnswer;
//
//class Solution_289 {
//    public void gameOfLife(int[][] board) {
//        //不利用额外空间，原地修改数组。利用状态，0，死，1，活，-1，之前活现在死 2，之前死，现在活
//        int[] neighbors = new int[]{-1,0,1};
//        int m = board.length;
//        int n = board[0].length;
//
//        for (int i = 0; i < m; i++) {
//            for (int j = 0; j < n; j++) {
//                int count = 0;//周围存活个数
//
//                for (int k = 0; k < 3; k++) {
//                    for (int l = 0; l < 3; l++) {
//                        if(!(neighbors[k]==0&&neighbors[l]==0)){
//                            int row = i+neighbors[k];
//                            int col = j+neighbors[l];
//
//                            if((row>=0&&row<m)&&(col>=0&&col<n)&&Math.abs(board[row][col])==1){
//                                count++;
//                            }
//                        }
//                    }
//                }
//
//                //第一次遍历，状态只可能为0，1
//                if(board[i][j]==0){
//                    board[i][j] = count==3?2:0;
//                }else {
//                    if(count==2||count==3){
//                        board[i][j] = 1;
//                    }else {
//                        board[i][j] = -1;
//                    }
//                }
//            }
//        }
//
//
//        for (int i = 0; i < m; i++) {
//            for (int j = 0; j < n; j++) {
//                if(board[i][j]==1||board[i][j]==2){
//                    board[i][j]=1;
//                }else {
//                    board[i][j]=0;
//                }
//            }
//        }
//    }
//}
//
////class Solution {
////    public void gameOfLife(int[][] board) {
////        int m = board.length;
////        int n = board[0].length;
////        int[][] pre = new int[m][n];
////        for (int i = 0; i < m; i++) {
////            pre[i] = Arrays.copyOf(board[i],n);
////        }
////
////        for (int i = 0; i < m; i++) {
////            for (int j = 0; j < n; j++) {
////                int count = 0;
////                if(i>0&&j>0&&pre[i-1][j-1]==1)count++;//左上
////                if(i>0&&pre[i-1][j]==1)count++;//上
////                if(i>0&&j<n-1&&pre[i-1][j+1]==1)count++;//右上
////                if(j<n-1&&pre[i][j+1]==1)count++;//右
////                if(i<m-1&&j<n-1&&pre[i+1][j+1]==1)count++;//右下
////                if(i<m-1&&pre[i+1][j]==1)count++;//下
////                if(i<m-1&&j>0&&pre[i+1][j-1]==1)count++;//左下
////                if(j>0&&pre[i][j-1]==1)count++;//左
////
////                if(pre[i][j]==0){
////                    board[i][j] = count==3?1:0;
////                }else {
////                    if(count==2||count==3){
////                        board[i][j]=1;
////                    } else {
////                        board[i][j]=0;
////                    }
////                }
////            }
////        }
////
////    }
////}